﻿#region Copyright & licence

// This file is part of Refix.
// 
// Refix is free software: you can redistribute it and/or modify
// it under the terms of the GNU Lesser General Public License as
// published by the Free Software Foundation, either version 3 of the
// License, or (at your option) any later version.
// 
// Refix is distributed in the hope that it will be useful,
// but WITHOUT ANY WARRANTY; without even the implied warranty of
// MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
// GNU Lesser General Public License for more details.
// 
// You should have received a copy of the GNU Lesser General Public
// License along with Refix.  If not, see <http://www.gnu.org/licenses/>.
// 
// Copyright (C) 2010-11 David Musgrove and others.

#endregion

using System;
using System.IO;
using System.Reflection;
using System.Text.RegularExpressions;

using NUnit.Framework;

using Refix.Core.Properties;

namespace Refix.Core.Tests
{
    [TestFixture]
    public class ResourceTest
    {
        [Test]
        public void Check_All_Resource_Strings_Exists()
        {
            var resourceRegex = new Regex("ResourceDispenser\\.GetString\\(\"(?<name>[A-Z_]+)\"\\)");
            var rootDirectory = new DirectoryInfo(Path.GetDirectoryName(Assembly.GetExecutingAssembly().Location))
                .Parent.Parent.Parent.GetDirectories("Refix.Core")[0];
            bool allFound = true;
            bool anyFound = false;
            foreach (var sourceFile in rootDirectory.GetFiles("*.cs", SearchOption.AllDirectories))
            {
                foreach (var line in File.ReadAllLines(sourceFile.FullName))
                {
                    var match = resourceRegex.Match(line);
                    if (match.Success)
                    {
                        anyFound = true;
                        string name = match.Groups["name"].Value;
                        string result = ResourceDispenser.GetString(name);
                        bool resultFound = !string.IsNullOrEmpty(result);
                        allFound &= resultFound;
                        if (!resultFound)
                        {
                            Console.WriteLine(sourceFile.FullName);
                            Console.WriteLine(name + ": " + (string.IsNullOrEmpty(result) ? "not " : "") + "found");
                        }
                    }
                }
            }
            Assert.IsTrue(allFound, "Not all resources named were found.");
            Assert.IsTrue(anyFound, "No resources found - do you have shadow copying enabled? It must be turned off.");
        }
    }
}